package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.app.utils.UmengEvent;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.index.fragment.FragmentBaike;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class IndexBaikeActivity extends BaseFragmentActivity implements
        OnTopbarNewLeftLayoutListener {

    private ViewPager mViewPager = null;
    private ArrayList<LinearLayout> mTabLayoutList;
    private ArrayList<ImageView> mTabPointList;

    public ArrayList<Fragment> fragmentList;
    public BeanIndexNew mIndexNew;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_baike_activity);

        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 添加页面动画，需要重写这个方法，
     */
    @Override
    public void onBackPressed() {
        finish(true);
    }

    @Override
    public void onTopbarLeftLayoutSelected() {
        finish(true);
    }

    /**
     * 初始化方法
     */
    private void init() {
        // topbar
        CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
        topbar.setTopbarTitle("同心百科");
        topbar.setonTopbarNewLeftLayoutListener(this);

        mIndexNew = (BeanIndexNew) getIntent().getSerializableExtra("indexNew");

        mTabLayoutList = new ArrayList<LinearLayout>();
        mTabPointList = new ArrayList<ImageView>();

        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout1));
        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout2));
        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout3));
        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout4));
        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout5));
        mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout6));

        mTabPointList.add((ImageView) findViewById(R.id.tab_point1));
        mTabPointList.add((ImageView) findViewById(R.id.tab_point2));
        mTabPointList.add((ImageView) findViewById(R.id.tab_point3));
        mTabPointList.add((ImageView) findViewById(R.id.tab_point4));
        mTabPointList.add((ImageView) findViewById(R.id.tab_point5));
        mTabPointList.add((ImageView) findViewById(R.id.tab_point6));

        //layout click
        for (int i = 0; i < mTabLayoutList.size(); i++) {
            mTabLayoutList.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int k = 0; k < mTabLayoutList.size(); k++) {
                        if (mTabLayoutList.get(k).getId() == v.getId()) {
                            mTabLayoutList.get(k).setSelected(true);
                            mViewPager.setCurrentItem(k, false);
                        } else {
                            mTabLayoutList.get(k).setSelected(false);
                        }
                    }
                }
            });
        }

        //show point
        if (mIndexNew != null) {
            if (mIndexNew.good > 0) {
                mTabPointList.get(0).setVisibility(View.VISIBLE);
            }
            if (mIndexNew.drug > 0) {
                mTabPointList.get(1).setVisibility(View.VISIBLE);
            }
            if (mIndexNew.food > 0) {
                mTabPointList.get(2).setVisibility(View.VISIBLE);
            }
            if (mIndexNew.sport > 0) {
                mTabPointList.get(3).setVisibility(View.VISIBLE);
            }
            if (mIndexNew.heart > 0) {
                mTabPointList.get(4).setVisibility(View.VISIBLE);
            }
            if (mIndexNew.life > 0) {
                mTabPointList.get(5).setVisibility(View.VISIBLE);
            }

        }

        //add fragment
        fragmentList = new ArrayList<Fragment>();
        FragmentBaike bbs1 = new FragmentBaike();
        FragmentBaike bbs2 = new FragmentBaike();
        FragmentBaike bbs3 = new FragmentBaike();
        FragmentBaike bbs4 = new FragmentBaike();
        FragmentBaike bbs5 = new FragmentBaike();
        FragmentBaike bbs6 = new FragmentBaike();

        Bundle data1 = new Bundle();
        data1.putInt("Type", 25);
        bbs1.setArguments(data1);

        Bundle data2 = new Bundle();
        data2.putInt("Type", 19);
        bbs2.setArguments(data2);

        Bundle data3 = new Bundle();
        data3.putInt("Type", 20);
        bbs3.setArguments(data3);

        Bundle data4 = new Bundle();
        data4.putInt("Type", 21);
        bbs4.setArguments(data4);

        Bundle data5 = new Bundle();
        data5.putInt("Type", 22);
        bbs5.setArguments(data5);

        Bundle data6 = new Bundle();
        data6.putInt("Type", 23);
        bbs6.setArguments(data6);

        fragmentList.add(bbs1);
        fragmentList.add(bbs2);
        fragmentList.add(bbs3);
        fragmentList.add(bbs4);
        fragmentList.add(bbs5);
        fragmentList.add(bbs6);

        mViewPager = (ViewPager) findViewById(R.id.motherCircleListViewPager);
        mViewPager.setAdapter(new FragmentPageAdapter(getSupportFragmentManager(), fragmentList, null, null));
        mViewPager.setOnPageChangeListener(new MyViewChangeListener());
        mViewPager.setOffscreenPageLimit(5);

        mViewPager.setCurrentItem(0, false);
        mTabLayoutList.get(0).setSelected(true);
        mTabPointList.get(0).setVisibility(View.GONE);
    }


    private void tongjiEvent(int index) {
        if (index == 0) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_jingxuan);
        } else if (index == 1) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_yongyao);
        }else if (index == 2) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_yinshi);
        }else if (index == 3) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_yundong);
        }else if (index == 4) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_xinli);
        }else if (index == 5) {
            UmengHelper.CustomEvent(IndexBaikeActivity.this, UmengEvent.event_tongxinbaike_shenghuo);
        }
    }

    class MyViewChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int arg2) {

        }

        @Override
        public void onPageSelected(int index) {

            tongjiEvent(index);
            mViewPager.setCurrentItem(index, false);
            mTabPointList.get(index).setVisibility(View.GONE);

            for (int i = 0; i < mTabLayoutList.size(); i++) {
                if (i == index) {
                    mTabLayoutList.get(i).setSelected(true);
                } else {
                    mTabLayoutList.get(i).setSelected(false);
                }
            }


        }
    }

}
